﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Chengqbd.Core.Common
{
	public static class EncryptProvider
	{
		private static string GetRandomStr(int length)
		{
			char[] array = new char[]
			{
				'a',
				'b',
				'd',
				'c',
				'e',
				'f',
				'g',
				'h',
				'i',
				'j',
				'k',
				'l',
				'm',
				'n',
				'p',
				'r',
				'q',
				's',
				't',
				'u',
				'v',
				'w',
				'z',
				'y',
				'x',
				'0',
				'1',
				'2',
				'3',
				'4',
				'5',
				'6',
				'7',
				'8',
				'9',
				'A',
				'B',
				'C',
				'D',
				'E',
				'F',
				'G',
				'H',
				'I',
				'J',
				'K',
				'L',
				'M',
				'N',
				'Q',
				'P',
				'R',
				'T',
				'S',
				'V',
				'U',
				'W',
				'X',
				'Y',
				'Z'
			};
			StringBuilder stringBuilder = new StringBuilder();
			Random random = new Random(DateTime.Now.Millisecond);
			for (int i = 0; i < length; i++)
			{
				stringBuilder.Append(array[random.Next(0, array.Length)].ToString());
			}
			return stringBuilder.ToString();
		}
        /// <summary>
        /// 创建AES
        /// </summary>
        /// <returns></returns>
		public static AESKey CreateAesKey()
		{
			return new AESKey
			{
				Key = GetRandomStr(32),
				IV = GetRandomStr(16)
			};
		}
        /// <summary>
        /// AES加密
        /// </summary>
        /// <param name="data"></param>
        /// <param name="key"></param>
        /// <param name="vector"></param>
        /// <returns></returns>
		public static string AESEncrypt(string data, string key, string vector)
		{
			byte[] bytes = Encoding.UTF8.GetBytes(data);
			byte[] array = AESEncrypt(bytes, key, vector);
			bool flag = array == null;
			string result;
			if (flag)
			{
				result = null;
			}
			else
			{
				result = Convert.ToBase64String(array);
			}
			return result;
		}
        /// <summary>
        /// AES加密 
        /// </summary>
        /// <param name="data"></param>
        /// <param name="key"></param>
        /// <param name="vector"></param>
        /// <returns></returns>
		public static byte[] AESEncrypt(byte[] data, string key, string vector)
		{
			byte[] array = new byte[32];
			Array.Copy(Encoding.UTF8.GetBytes(key.PadRight(array.Length)), array, array.Length);
			byte[] array2 = new byte[16];
			Array.Copy(Encoding.UTF8.GetBytes(vector.PadRight(array2.Length)), array2, array2.Length);
			byte[] array3 = null;
			byte[] result;
			using (Aes aes = Aes.Create())
			{
				try
				{
					using (MemoryStream memoryStream = new MemoryStream())
					{
						using (CryptoStream cryptoStream = new CryptoStream(memoryStream, aes.CreateEncryptor(array, array2), CryptoStreamMode.Write))
						{
							cryptoStream.Write(data, 0, data.Length);
							cryptoStream.FlushFinalBlock();
							array3 = memoryStream.ToArray();
						}
					}
				}
				catch
				{
					array3 = null;
				}
				result = array3;
			}
			return result;
		}
        /// <summary>
        /// AES解密
        /// </summary>
        /// <param name="data"></param>
        /// <param name="key"></param>
        /// <param name="vector"></param>
        /// <returns></returns>
		public static string AESDecrypt(string data, string key, string vector)
		{
			byte[] data2 = Convert.FromBase64String(data);
			byte[] array = AESDecrypt(data2, key, vector);
			bool flag = array == null;
			string result;
			if (flag)
			{
				result = null;
			}
			else
			{
				result = Encoding.UTF8.GetString(array);
			}
			return result;
		}
        /// <summary>
        /// AES解密
        /// </summary>
        /// <param name="data"></param>
        /// <param name="key"></param>
        /// <param name="vector"></param>
        /// <returns></returns>
		public static byte[] AESDecrypt(byte[] data, string key, string vector)
		{
			byte[] array = new byte[32];
			Array.Copy(Encoding.UTF8.GetBytes(key.PadRight(array.Length)), array, array.Length);
			byte[] array2 = new byte[16];
			Array.Copy(Encoding.UTF8.GetBytes(vector.PadRight(array2.Length)), array2, array2.Length);
			byte[] array3 = null;
			byte[] result;
			using (Aes aes = Aes.Create())
			{
				try
				{
					using (MemoryStream memoryStream = new MemoryStream(data))
					{
						using (CryptoStream cryptoStream = new CryptoStream(memoryStream, aes.CreateDecryptor(array, array2), CryptoStreamMode.Read))
						{
							using (MemoryStream memoryStream2 = new MemoryStream())
							{
								byte[] array4 = new byte[1024];
								int count;
								while ((count = cryptoStream.Read(array4, 0, array4.Length)) > 0)
								{
									memoryStream2.Write(array4, 0, count);
								}
								array3 = memoryStream2.ToArray();
							}
						}
					}
				}
				catch
				{
					array3 = null;
				}
				result = array3;
			}
			return result;
		}
        /// <summary>
        /// AES加密
        /// </summary>
        /// <param name="data"></param>
        /// <param name="key"></param>
        /// <returns></returns>
		public static string AESEncrypt(string data, string key)
		{
			string result;
			using (MemoryStream memoryStream = new MemoryStream())
			{
				using (Aes aes = Aes.Create())
				{
					byte[] bytes = Encoding.UTF8.GetBytes(data);
					byte[] array = new byte[32];
					Array.Copy(Encoding.UTF8.GetBytes(key.PadRight(array.Length)), array, array.Length);
					aes.Mode = CipherMode.ECB;
					aes.Padding = PaddingMode.PKCS7;
					aes.KeySize = 128;
					aes.Key = array;
					using (CryptoStream cryptoStream = new CryptoStream(memoryStream, aes.CreateEncryptor(), CryptoStreamMode.Write))
					{
						try
						{
							cryptoStream.Write(bytes, 0, bytes.Length);
							cryptoStream.FlushFinalBlock();
							result = Convert.ToBase64String(memoryStream.ToArray());
						}
						catch (Exception)
						{
							result = null;
						}
					}
				}
			}
			return result;
		}
        /// <summary>
        /// AES加密
        /// </summary>
        /// <param name="data"></param>
        /// <param name="key"></param>
        /// <returns></returns>
		public static string AESDecrypt(string data, string key)
		{
			byte[] array = Convert.FromBase64String(data);
			byte[] array2 = new byte[32];
			Array.Copy(Encoding.UTF8.GetBytes(key.PadRight(array2.Length)), array2, array2.Length);
			string result;
			using (MemoryStream memoryStream = new MemoryStream(array))
			{
				using (Aes aes = Aes.Create())
				{
					aes.Mode = CipherMode.ECB;
					aes.Padding = PaddingMode.PKCS7;
					aes.KeySize = 128;
					aes.Key = array2;
					using (CryptoStream cryptoStream = new CryptoStream(memoryStream, aes.CreateDecryptor(), CryptoStreamMode.Read))
					{
						try
						{
							byte[] array3 = new byte[array.Length];
							int num = cryptoStream.Read(array3, 0, array.Length);
							byte[] array4 = new byte[num];
							Array.Copy(array3, 0, array4, 0, num);
							result = Encoding.UTF8.GetString(array4);
						}
						catch (Exception)
						{
							result = null;
						}
					}
				}
			}
			return result;
		}
        /// <summary>
        /// MD5 加密 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="length"></param>
        /// <returns></returns>
		public static string Md5(string value, MD5Length length = MD5Length.L32)
		{
			bool flag = string.IsNullOrWhiteSpace(value);
			string result;
			if (flag)
			{
				result = string.Empty;
			}
			else
			{
				MD5CryptoServiceProvider md5CryptoServiceProvider = new MD5CryptoServiceProvider();
				string text;
				try
				{
					byte[] value2 = md5CryptoServiceProvider.ComputeHash(Encoding.UTF8.GetBytes(value));
					text = ((length == MD5Length.L32) ? BitConverter.ToString(value2) : BitConverter.ToString(value2, 4, 8));
				}
				finally
				{
					if (md5CryptoServiceProvider != null)
					{
						md5CryptoServiceProvider.Clear();
					}
					if (md5CryptoServiceProvider != null)
					{
						md5CryptoServiceProvider.Dispose();
					}
				}
				result = text.Replace("-", "");
			}
			return result;
		}
        /// <summary>
        /// MD5 加密 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="encoding"></param>
        /// <param name="length"></param>
        /// <returns></returns>
		public static string Md5(string value, Encoding encoding, MD5Length length = MD5Length.L32)
		{
			bool flag = string.IsNullOrWhiteSpace(value);
			string result;
			if (flag)
			{
				result = string.Empty;
			}
			else
			{
				MD5CryptoServiceProvider md5CryptoServiceProvider = new MD5CryptoServiceProvider();
				string text;
				try
				{
					byte[] value2 = md5CryptoServiceProvider.ComputeHash(encoding.GetBytes(value));
					text = ((length == MD5Length.L32) ? BitConverter.ToString(value2) : BitConverter.ToString(value2, 4, 8));
				}
				finally
				{
					if (md5CryptoServiceProvider != null)
					{
						md5CryptoServiceProvider.Clear();
					}
					if (md5CryptoServiceProvider != null)
					{
						md5CryptoServiceProvider.Dispose();
					}
				}
				result = text.Replace("-", "");
			}
			return result;
		}
	}
}
